home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-07-17 | 13.4 KB | 447 lines | [TEXT/ttxt] |
- unit DemoUtils;
-
-
- interface
-
-
- uses
- About, { …my unit! }
- Globals; { program globals }
-
-
-
- function aNum2Str (aNum: LongInt): Str255;
- { NumToString procedure available as a function }
-
- function aStr2Num (NumStr: Str255): Integer;
- { StringToNum procedure available as a function }
- { Note: won't accurately return numbers if > 32767 or if letters are in NumStr }
-
- function CtrlEnabled (theDialog: DialogPtr;
- whichItem: Integer): Boolean;
-
- procedure DrawDefaultBtn (theDialog: DialogPtr;
- Item: Integer);
- { outline default button in any dialog window }
-
- procedure FixWindowColor (theWindow: DialogPtr);
- { set window background color to match custom colored window content fill }
-
- procedure CenterWindow (theDialog: DialogPtr);
- { Center window - center higher for large screens - show, set port }
-
- procedure FakeClick (theDialog: DialogPtr;
- theButton: Integer);
- { select/deselect a button in a dialog }
-
- procedure SetBtnTitle (theDialog: DialogPtr;
- Btn: Integer;
- Title: Str255);
- { update button title for dialog }
-
- procedure SetCheckOrRadioBtn (theDialog: DialogPtr;
- Btn, BtnState: Integer);
- { update radio or check button status for dialog }
-
- function GetEdText (theDialog: DialogPtr;
- Which: Integer): Str255;
- { return edit text contents }
-
- procedure ChangeChoiceText (theDialog: DialogPtr;
- Which: Integer;
- Msg: Str255);
- { change edit text contents }
-
- function TabSelectText (theDialog: DialogPtr;
- direction: Integer): Boolean;
- { select the next, previous, or only edit text field }
- { returns true if a field was found and selected }
-
- function ShiftDown: Boolean;
-
- procedure myDrawSICN (theID, resOffset: Integer;
- theRect: Rect);
- { draw SICN, placing topleft of SICN in topleft of theRect }
-
- procedure VertCenterRect (var theRect: Rect;
- mainRect: Rect);
-
- procedure UpdatePopUp (theDialog: DialogPtr;
- var aPopRec: PopUpMenu);
- { select/deselect a btn in a dialog }
-
- function HandlePopUpSelect (theDialog: DialogPtr;
- var aPopRec: PopUpMenu): Boolean;
- { deal with popup menu selection }
-
-
-
- implementation
-
-
-
- function aNum2Str (aNum: LongInt): Str255;
- { NumToString procedure available as a function }
- var
- NumStr: Str255;
- begin
- NumToString(aNum, NumStr);
- aNum2Str := NumStr;
- end; { of func aNum2Str }
-
-
- function aStr2Num (NumStr: Str255): Integer;
- { StringToNum procedure available as a function }
- { Note: won't accurately return numbers if > 32767 or if letters are in NumStr }
- var
- aNum: LongInt;
- begin
- StringToNum(Copy(NumStr, 1, 5), aNum);
- if aNum < maxInt then
- aStr2Num := aNum
- else
- aStr2Num := maxInt;
- end; { of func aStr2Num }
-
-
- function CtrlEnabled (theDialog: DialogPtr;
- whichItem: Integer): Boolean;
- var
- thetype: Integer;
- itmHdl: Handle;
- itmrect: Rect;
- begin
- GetDItem(theDialog, whichItem, theType, itmHdl, itmrect);{ get button junk }
- CtrlEnabled := (itmHdl <> nil) & (ControlHandle(itmHdl)^^.contrlHilite <> Disable);
- end; { of proc CtrlEnabled }
-
-
- procedure DrawDefaultBtn (theDialog: DialogPtr;
- Item: Integer);
- { outline default button in any dialog window }
- var
- theInt: Integer;
- btnHdl: Handle;
- thePen: PenState;
- btnrect: Rect;
- begin
- SetPort(theDialog); { set window to current graf port }
- GetPenState(thePen); { save current pen }
- if (theDialog <> FrontWindow) | (not CtrlEnabled(theDialog, DialogPeek(theDialog)^.aDefItem)) then
- PenPat(gray);
- GetDItem(theDialog, DialogPeek(theDialog)^.aDefItem, theInt, btnHdl, btnrect); { get item location }
- Pensize(3, 3); { no wimpy button outlines here }
- InsetRect(btnrect, -4, -4); { set rectangle around button }
- FrameRoundRect(btnrect, 16, 16); { draw the sucker! }
- SetPenState(thePen); { restore old pen }
- end; { of proc DrawDefaultBtn }
-
-
- function GetAuxWin (theWindow: WindowPtr;
- var awHndl: AuxWinHandle): Boolean;
- inline
- $AA42;
-
-
- procedure FixWindowColor (theWindow: DialogPtr);
- { set window background color to match custom colored window content fill }
- var
- usedDefaultColors: Boolean;
- theWorld: SysEnvRec;
- RGBbackground: RGBColor;
- awHndl: AuxWinHandle;
- savePort: GrafPtr;
- begin
- if (SysEnvirons(1, theWorld) <> envNotPresent) then { SysEnvirons call is available }
- if theWorld.hasColorQD then { has Color QuickDraw - OK to look for window color record… }
- begin
- GetPort(savePort);
- usedDefaultColors := GetAuxWin(theWindow, awHndl);
- RGBbackground := awHndl^^.awCTable^^.ctTable[cFrameColor].rgb;
- RGBBackColor(RGBbackground); { set background to match wContentColor when drawing }
- SetPort(theWindow);
- EraseRect(theWindow^.portRect);
- SetPort(savePort);
- end;
- end; { of proc FixWindowColor }
-
-
- procedure CenterWindow (theDialog: DialogPtr);
- { Center window - center higher for large screens - show, set port }
- var
- usedDefaultColors: Boolean;
- theWorld: SysEnvRec;
- RGBbackground: RGBColor;
- awHndl: AuxWinHandle;
- begin
- SetPort(theDialog); { set window to current graf port }
- with screenBits, theDialog^ do
- MoveWindow(theDialog, ((bounds.right - bounds.left - portrect.right + portrect.left) div 2), ((bounds.bottom - bounds.top - portrect.bottom + portrect.top + 20) div 3), True);
-
- ShowWindow(theDialog);
- FixWindowColor(theDialog);
- end; { of proc CenterWindow }
-
-
- procedure FakeClick (theDialog: DialogPtr;
- theButton: Integer);
- { select/deselect a button in a dialog }
- var
- theInt: Integer;
- LInt: LongInt;
- btnHdl: Handle;
- btnrect: Rect;
- begin
- GetDItem(theDialog, theButton, theInt, btnHdl, btnrect);
- HiliteControl(ControlHandle(btnHdl), 1);
- Delay(8, LInt);
- HiliteControl(ControlHandle(btnHdl), 0);
- end; { of proc FakeClick }
-
-
- procedure SetBtnTitle (theDialog: DialogPtr;
- Btn: Integer;
- Title: Str255);
- { update button title for dialog }
- var
- itmNum: Integer;
- itmRect: Rect;
- CurTitle: Str255;
- itmHdl: Handle;
- begin
- GetDItem(theDialog, Btn, itmNum, itmHdl, itmRect); { get button junk }
- GetCTitle(ControlHandle(itmHdl), CurTitle); { get current title }
- if Title <> CurTitle then
- SetCTitle(ControlHandle(itmHdl), Title); { set title }
- end; { of proc SetBtnTitle }
-
-
- procedure SetCheckOrRadioBtn (theDialog: DialogPtr;
- Btn, BtnState: Integer);
- { update radio or check button status for dialog }
- var
- thetype: Integer;
- itmrect: Rect;
- itmHdl: Handle;
- begin
- GetDItem(theDialog, Btn, theType, itmHdl, itmrect); { get button junk }
- if itmHdl = nil then
- Exit(SetCheckOrRadioBtn);
- if BtnState <> Disable then
- begin
- HiliteControl(ControlHandle(itmHdl), Off); { enable control }
- SetCtlValue(ControlHandle(itmHdl), BtnState) { set button state }
- end
- else
- HiliteControl(ControlHandle(itmHdl), BtnState); { disable control }
- end; { of proc SetCheckOrRadioBtn }
-
-
- function GetEdText (theDialog: DialogPtr;
- Which: Integer): Str255;
- { return edit text contents }
- var
- itmNum: Integer;
- itmrect: Rect;
- itmHdl: Handle;
- Msg: Str255;
- begin
- GetDItem(theDialog, Which, itmNum, itmHdl, itmrect);
- GetIText(itmHdl, Msg);
- GetEdText := Msg;
- end; { of func GetEdText }
-
-
- procedure ChangeChoiceText (theDialog: DialogPtr;
- Which: Integer;
- Msg: Str255);
- { change edit text contents }
- var
- itmNum: Integer;
- itmrect: Rect;
- itmHdl: Handle;
- begin
- if GetEdText(theDialog, Which) <> Msg then { check current text before updating... }
- begin
- GetDItem(theDialog, Which, itmNum, itmHdl, itmrect);
- SetIText(itmHdl, Msg); { ...to avoid flicker }
- end;
- end; { of proc ChangeChoiceText }
-
-
- function TabSelectText (theDialog: DialogPtr;
- direction: Integer): Boolean;
- { select the next, previous, or only edit text field }
- { returns true if a field was found and selected }
- var
- thePtr: ^Integer;
- x, theItem, totItems, itmtype: Integer;
- itmHdl: Handle;
- itmrect: Rect;
- begin
- TabSelectText := False;
- theItem := 0;
- x := Succ(DialogPeek(theDialog)^.editField); { current edit text item }
- if x = 0 then
- Exit(TabSelectText); { no edit text fields in dialog! }
- thePtr := Pointer(DialogPeek(theDialog)^.Items^);
- totItems := 1 + thePtr^; { total # of items in dialog }
- while theItem = 0 do
- begin
- x := x + direction;
- if x > totItems then
- x := 1; { reset index to first item }
- if x < 1 then
- x := totItems; { reset index to last item }
- GetDItem(theDialog, x, itmtype, itmHdl, itmrect); { get item's rect }
- if (itmtype = editText) or (itmtype = editText + itemDisable) then
- theItem := x; { found an edit text item }
- end;
- SelIText(theDialog, theItem, 0, maxint); { select ALL edit text }
- TabSelectText := True;
- end; { of func TabSelectText }
-
-
- function ShiftDown: Boolean;
- var
- keys: keymap;
- begin
- GetKeys(keys);
- shiftdown := bittst(@keys, 63);
- end;
-
-
- procedure myDrawSICN (theID, resOffset: Integer;
- theRect: Rect);
- { draw SICN, placing topleft of SICN in topleft of theRect }
- var
- theResource: Handle;
- theBits: BitMap;
- byteCount: integer;
- tempPort: GrafPtr;
- begin
- theResource := GetResource('SICN', theID);
- if (theResource <> nil) then
- begin
- SetRect(theBits.bounds, theRect.left, theRect.top, theRect.left + 16, theRect.top + 16);
- theBits.rowBytes := (((theBits.bounds.right - theBits.bounds.left) + 15) div 16) * 2;
- byteCount := Longint(theBits.bounds.bottom - theBits.bounds.top) * longint(theBits.rowBytes);{ Be sure it's a longint }
- theBits.baseAddr := Ptr(NewPtr(byteCount));
- if MemError = noErr then
- begin
- HLock(theResource);
- BlockMove(Ptr(Ord(theResource^) + (resOffset * 32)), theBits.baseAddr, 32); { move in 32 bits! }
- HUnlock(theResource);
- GetPort(tempPort);
- CopyBits(theBits, tempPort^.portBits, theBits.bounds, theBits.bounds, srcCopy, nil);{srcCopy srcOr}
- DisposPtr(theBits.baseAddr);
- end;
- ReleaseResource(theResource);
- end; {maybe we should do something on an error??}
- end;{ of proc myDrawSICN }
-
-
- procedure VertCenterRect (var theRect: Rect;
- mainRect: Rect);
- var
- offsetAmt: Integer;
- begin
- offsetAmt := ((mainRect.bottom - mainRect.top) - (theRect.bottom - theRect.top)) div 2;
- OffsetRect(theRect, 0, offsetAmt);
- end; { of proc VertCenterRect }
-
-
- procedure UpdatePopUp (theDialog: DialogPtr;
- var aPopRec: PopUpMenu);
- { select/deselect a btn in a dialog }
- var
- theIcon: Byte;
- i, Width: Integer;
- SICNrect, popRect: Rect;
- MenuLine: Str255;
- cmdChar: Char;
- fontStuff: FontInfo;
- begin
- SetPort(theDialog);
- GetFontInfo(fontStuff);
- GetItem(aPopRec.MenuHndl, aPopRec.Selected, MenuLine); { get selection text }
-
- { remove trailing spaces - trailing spaces (or option-spaces) are used to pad menu so it will be }
- { wide enough to avoid truncating of popup control text in window }
- {$push}
- {$R-}
- for i := Length(MenuLine) downto 1 do
- if (MenuLine[i] = Chr(32)) | (MenuLine[i] = Chr(202)) then
- MenuLine[0] := Chr(Pred(Ord(MenuLine[0])))
- else
- leave;
- {$pop}
- popRect := aPopRec.PopUpRect;
-
- EraseRect(popRect);
- FrameRect(popRect);
- MoveTo(popRect.left + 2, popRect.bottom);
- LineTo(popRect.right, popRect.bottom);
- LineTo(popRect.right, popRect.top + 2);
-
- GetItemCmd(aPopRec.MenuHndl, aPopRec.Selected, cmdChar); { check for SICN in menu }
- if Ord(cmdChar) = 30 then
- begin
- SetRect(SICNrect, popRect.left + 6, popRect.top, popRect.right, popRect.top + 16);
- VertCenterRect(SICNrect, popRect);
-
- GetItemIcon(aPopRec.MenuHndl, aPopRec.Selected, theIcon);
- myDrawSICN(256 + theIcon, 0, SICNrect);
- popRect.left := popRect.left + 20;
- end;
-
- Width := popRect.right - popRect.left - 18;
- if StringWidth(MenuLine) > Width then { simple truncating algorithm }
- begin
- MenuLine := Concat(MenuLine, '…');
- while StringWidth(MenuLine) > Width do
- Delete(MenuLine, Pred(Length(MenuLine)), 1);
- end;
- i := ((popRect.Bottom - popRect.Top) - (fontStuff.ascent + fontStuff.descent)) div 2;
- MoveTo(popRect.Left + 6, popRect.Top + fontStuff.ascent + i); { move to text position }
- DrawString(MenuLine);
-
- SetRect(SICNrect, popRect.right - 18, popRect.top, popRect.right, popRect.top + 16);
- VertCenterRect(SICNrect, popRect);
- myDrawSICN(popupSICNid, 0, SICNrect);
-
- CheckItem(aPopRec.MenuHndl, aPopRec.Selected, true);
- end; { of proc UpdatePopUp }
-
-
- function HandlePopUpSelect (theDialog: DialogPtr;
- var aPopRec: PopUpMenu): Boolean;
- { deal with popup menu selection }
- var
- Result: LongInt;
- MenuStr: Str255;
- theHdl: Handle;
- PopLoc: Rect;
- itemType: Integer;
- begin
- if aPopRec.canInvert then
- InvertRect(aPopRec.promptRect); { invert popupmenu prompt item }
- PopLoc := aPopRec.PopUpRect;
- LocalToGlobal(PopLoc.TopLeft);
- CalcMenuSize(aPopRec.MenuHndl); { Work around Menu Mgr bug }
- Result := PopUpMenuSelect(aPopRec.MenuHndl, PopLoc.TopLeft.v, PopLoc.TopLeft.h, aPopRec.Selected);
- if aPopRec.canInvert then
- InvertRect(aPopRec.promptRect); { invert popupmenu prompt item }
- if (LoWord(Result) > 0) and (LoWord(Result) <> aPopRec.Selected) then
- begin
- GetItem(aPopRec.MenuHndl, LoWord(Result), MenuStr); { get selection text }
- CheckItem(aPopRec.MenuHndl, aPopRec.Selected, False);
- aPopRec.Selected := LoWord(Result);
- HandlePopUpSelect := True;
- end
- else
- HandlePopUpSelect := False;
- end; { of func HandlePopUpSelect }
-
-
- end.